package com.jdiv.samples.castlevania.applet;

import com.jdiv.JDiv;
import com.jdiv.JProcess;

public class Simon extends JProcess {
	
	private int vel=5;
	private int run=100;
	private int dir=0;
	private int old_x=0;
	private int vSalto=15;
	private int grav=2;
	private int saltando=vSalto;
	private int last_y=0;
	private boolean hold=false;
	private boolean isSaltando=false;;

	public Simon() {
		this.file=Main.fpg_simon;
		this.graph=100;
		this.x=15;
		this.y=165;
	}

	@Override
	public void begin() {
		// TODO Auto-generated method stub

	}
	
	public void loop(){
	//	System.out.println("Castle:"+Main.castle.x);
	//	System.out.println("BPiedra:"+Main.bpiedra.x);
	//	System.out.println("Simon:"+x);
		
		if (key(_RIGHT)){
			if (!hold)  if(run>110) run=100; else graph=run;
			 run++;
			 x+=vel;
			 flags=0;
			 dir=0;
			 
			 if (Main.castle.x<300 && Main.castle.x>97) Main.bpiedra.x-=3;
			 
		}
		
		if (key(_LEFT)){
			if (!hold) if(run>110) run=100; else graph=run;
			 run++;
			 x-=vel;
			 flags=1;
			 dir=1;
			 
			 if (Main.castle.x>97 && Main.castle.x<300) Main.bpiedra.x+=3;
			 
		}
		
		if (key(_SPACE) && !hold) { graph=119; hold=true; }
			 
		
		
		if (hold){
			if (graph<=125){
				if (dir>0) flags=1;
				graph++;
				
			}else{
				graph=100;
				hold=false;
			}
		}
		
		if(key(_UP)){
			
			  if (!isSaltando){
				  last_y=y;
				  isSaltando=true;
			  }
		}
	  
		if (isSaltando){
			  saltando-=grav;
	     	  y=y-saltando;
	     	  	if (y==last_y){ isSaltando=false; saltando=vSalto;}
	  
		}
		
		if (x>JDiv.getWidth()/2){ 
			//System.out.println(Main.castle.x);
			//System.out.println(x);
			//-320 , 97
			if (dir==0 && Main.castle.x>97){ //
				x=old_x;
				Main.castle.x-=3;
			}
		}else if (x<JDiv.getWidth()/2){
			if (dir==1 && Main.castle.x>=97 && Main.castle.x<1281/2){
				x=old_x;
				Main.castle.x+=3;

			}
		}
		
		
		if (x>215) x=old_x;
		
		old_x=x;
	}

}
